package com.mmd.manage.service.impl;

import com.mmd.common.entity.MmdPostcode;
import com.mmd.manage.dao.MmdPostcodeDao;
import com.mmd.manage.service.PostCodeService;
import com.mmd.manage.vo.PostcodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author libra
 * @Date 2019/11/9 21:35
 * @Description
 **/
@Service
public class PostCodeServiceImpl implements PostCodeService {

    @Autowired
    private MmdPostcodeDao mmdPostcodeDao;

    @Override
    public List<PostcodeVo> getPostcodeList(Long areaId) {
        List<PostcodeVo> list = new ArrayList<>();
        MmdPostcode postcode = new MmdPostcode();
        postcode.setParentAreaId(areaId);
        List<MmdPostcode> mmdPostcodes = mmdPostcodeDao.selectByKeySelective(postcode);
        if (CollectionUtils.isEmpty(mmdPostcodes)) {
            return list;
        }
        PostcodeVo vo = null;
        List<MmdPostcode> collect = mmdPostcodes.stream().sorted(Comparator.comparing(MmdPostcode::getAreaId)).collect(Collectors.toList());
        for (MmdPostcode mmdPostcode : collect) {
            vo = new PostcodeVo();
            vo.setPostcode(mmdPostcode.getPostcode());
            vo.setShowName(mmdPostcode.getAreaName() + " > " + mmdPostcode.getPostcode());
            list.add(vo);
        }

        return list;
    }
}
